﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NCRGEA.Admin
{
    public partial class frmConvert90ToFree : Form
    {
        public frmConvert90ToFree()
        {
            InitializeComponent();
        }

        private void frmConvert90ToFree_Load(object sender, EventArgs e)
        {
            RefreshData();
            foreach (DataGridViewColumn a in grdConvert90.Columns)
                a.ReadOnly = true;
            grdConvert90.Columns[0].ReadOnly = false;
        }

        private void RefreshData()
        {
            string sqlQry = @"SELECT i.MemberID, t.Name, i.LastName, i.FirstName, i.Sex, i.DOB, i.City, i.State, i.SSN FROM memInfo i INNER JOIN memTypes t ON i.TypeID=t.TypeID WHERE DATEDIFF(YEAR,i.DOB,GETDATE()) >= 90 AND i.TypeID IN (1,4,6) AND i.DOB <> '0001-01-01'";
            DataTable results = AAL.DAL.ExecuteQuery(sqlQry);
            grdConvert90.DataSource = results;
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow d in grdConvert90.Rows)
                d.Cells[0].Value = 1;
        }

        private void btnSelectNone_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow d in grdConvert90.Rows)
                d.Cells[0].Value = 0;
        }

        private void txtConvertToFreeStatus_Click(object sender, EventArgs e)
        {
            List<string> memberIDs = new List<string>();
            foreach (DataGridViewRow d in grdConvert90.Rows)
            {
                if(d.Cells[0].Value != null)
                    if (d.Cells[0].Value.ToString() == "1")
                        memberIDs.Add(d.Cells[1].Value.ToString());
            }
            string all = string.Join(",", memberIDs.ToArray());
            string sqlQry = @"UPDATE memInfo SET TypeID=3 WHERE MemberID IN (" + all + ")";
            int result = AAL.DAL.ExecuteNonQuery(sqlQry);
            RefreshData();
        }
    }
}
